
set quoted_identifier off
GO
--sp_Com_Trae_Orden_Compra_2 '01','01/07/2013','31/07/2013','S','2013','07','03','*','co03codpro','10000000001'
ALTER PROCEDURE Sp_Com_Trae_Orden_Compra_2         
@cCodemp Varchar(2),        
@cFecIni varchar(10),        
@cFecFin varchar(10),
@cmoneda varchar(1),
@cano varchar(4),
@cmes varchar(2),        
@cTipAna varchar(2),        
@cTipOrden varchar(1),        
@cCampo varchar(30),        
@cFiltro varchar(50)        
        
As        
        
IF @cTipOrden <> '*'        
BEGIN        
 IF @cFiltro = '*'        
 BEGIN        
  Execute("        
  Select Co03Docu.*,Ccm02Cta.*,        
      'Prov'=(SELECT COUNT(*) FROM CO05DOCU WHERE        
      CO05CODEMP = CO03CODEMP        
     AND CO05TIPO   = CO03TIPO         
      AND CO05CODIGO  = CO03CODIGO)         
       From         
             Co03Docu, Ccm02Cta        
             Where Co03Codemp = '" +  @cCodemp+ "'        
             And CO03FECHA >= Convert(DateTime, '" + @cFecIni + "',103)         
             And CO03FECHA <= Convert(DateTime, '" + @cFecFin + "',103)         
             And Ccm02Emp =  '" +  @cCodemp+ "'        
             And Co03CodPro = Ccm02Cod         
             And Ccm02TipAna = '" + @cTipAna + "'        
             AND Co03Tipo = '" + @cTipOrden + "'         
             AND co03codarea <> 'T'          
      Order By " + @cCampo)        
 END        
 ELSE        
 BEGIN        
  Execute("         
    Select Co03Docu.*,Ccm02Cta.*,        
        'Prov'=(SELECT COUNT(*) FROM CO05DOCU WHERE        
      CO05CODEMP = CO03CODEMP        
      AND CO05AA     = CO03AA        
      AND CO05CODIGO  = CO03CODIGO)         
     From         
            Co03Docu, Ccm02Cta         
            Where Co03Codemp = '" +  @cCodemp+ "'        
            And CO03FECHA >= Convert(DateTime, '" + @cFecIni + "',103)         
            And CO03FECHA <= Convert(DateTime, '" + @cFecFin + "',103)         
            And Ccm02Emp =  '" +  @cCodemp+ "'        
            And Co03CodPro = Ccm02Cod        
            And Ccm02TipAna = '" + @cTipAna + "'        
            AND Co03Tipo = '" + @cTipOrden + "'         
            AND co03codarea <> 'T'          
     AND Substring(" + @cCampo + ",1,Len('" + @cFiltro + "' )) = '" + @cFiltro + "'         
             Order By " + @cCampo)        
 END        
END        
ELSE        
BEGIN        
 IF @cFiltro = '*'        
 BEGIN        
  Execute("         
    Select Co03Docu.*,Ccm02Cta.* ,        
           'Prov'=(SELECT COUNT(*) FROM CO05DOCU WHERE        
      CO05CODEMP = CO03CODEMP        
      AND CO05AA     = CO03AA        
      AND CO05CODIGO  = CO03CODIGO) From         
             Co03Docu, Ccm02Cta         
             Where Co03Codemp = '" +  @cCodemp+ "'        
             And CO03FECHA >= Convert(DateTime, '" + @cFecIni + "',103)        
             And CO03FECHA <= Convert(DateTime, '" + @cFecFin + "',103)        
             And Ccm02Emp =  '" +  @cCodemp+ "'        
             And Co03CodPro = Ccm02Cod         
             And Ccm02TipAna = '" + @cTipAna + "'        
             AND co03codarea <> 'T'          
             Order By " + @cCampo)        
 END        
 ELSE        
 BEGIN        
  Execute("         
  Select Co03Docu.*,Ccm02Cta.*,        
    'Prov'=(SELECT COUNT(*) FROM CO05DOCU WHERE        
      CO05CODEMP = CO03CODEMP        
      AND CO05AA     = CO03AA        
      AND CO05CODIGO  = CO03CODIGO)        
   From         
          Co03Docu, Ccm02Cta         
          Where Co03Codemp = '" +  @cCodemp+ "'        
          And CO03FECHA >= Convert(DateTime, '" + @cFecIni + "',103)        
          And CO03FECHA <= Convert(DateTime, '" + @cFecFin + "',103)        
          And Ccm02Emp =  '" +  @cCodemp+ "'        
          And Co03CodPro = Ccm02Cod         
          And Ccm02TipAna = '" + @cTipAna + "'        
          AND co03codarea <> 'T'          
          AND Substring(" + @cCampo + ",1,Len('" + @cFiltro + "' )) = '" + @cFiltro + "'         
           Order By " + @cCampo)        
 END        
END        
        
GO
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
      
        
        
        
        
        
        
        
        
        
        
        
        
        
    
        
        
        
        
        
        
        
      
      
      
      
      
      
    
  
  
  
  
  